var BusCon = require("../../core/bus/_con");
var GoodsCon = require("../../core/goods/_con");
var TagCon = require("../../core/bus.tag/_con");
var ResponObj = require("../_include/responObj");
var role = require("./role");
module.exports = {
	prefix: "/bus/tag",
	get: {
		"/": require("./tag.rest.js").get["/"],
		"/detail": require("./tag.rest.js").get["/detail"],
		"/detail/by_tag_id": require("./tag.rest.js").get["/detail/by_tag_id"]
	},
	post: {
		"/": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var new_tag_name = body.tag_name;
			var tag_id = bus.addTag(new_tag_name);
			var tag = TagCon.get(tag_id);
			this.body = ResponObj("json", tag.getDetail());
		},
		"/add_goods": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var goods_id = body.goods_id;
			if (!(goods_id || tag_id)) {
				throwCusError("参数错误")
			}
			bus.addGoodsToTag(tag_id, goods_id);
			this.body = ResponObj("json", TagCon.get(tag_id).getDetail());
		},
	},
	put: {
		"/update_tag_index": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var tag_index = body.tag_index;
			this.body = ResponObj("json", bus.updateTagIndex(tag_id, tag_index));
		},
		"/addGoodsToTag": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var goods_id = body.goods_id;
			bus.addGoodsToTag(tag_id, goods_id);
			this.body = ResponObj("json", TagCon.get(tag_id));
		},
		"/removeGoodsFromTag": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var goods_id = body.goods_id;
			bus.removeGoodsFromTag(tag_id, goods_id);
			this.body = ResponObj("json", TagCon.get(tag_id));
		},
		"/addTagToGoods": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var goods_id = body.goods_id;
			bus.addGoodsToTag(tag_id, goods_id);
			this.body = ResponObj("json", GoodsCon.get(goods_id));
		},
		"/removeTagFromGoods": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var goods_id = body.goods_id;
			bus.removeGoodsFromTag(tag_id, goods_id);
			this.body = ResponObj("json", GoodsCon.get(goods_id));
		},
		"/rename": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			var new_tag_name = body.tag_name;
			bus.updateTagName(tag_id, new_tag_name);
			this.body = ResponObj("json", TagCon.get(tag_id).getDetail());
		}
	},
	"delete": {
		"/": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var tag_id = body.tag_id;
			if (!tag_id) {
				throwCusError("参数错误")
			}
			bus.removeTag(tag_id);
			var tags = bus.getTagsDetail();
			this.body = ResponObj("json", tags);
		},
		"/remove_goods": function() {
			var bus = role.getBus(this);
			var body = this.request.body;
			var goods_id = body.goods_id;
			var tag_id = body.tag_id;
			if (goods_id && tag_id) {
				bus.removeGoodsFromTag(tag_id, goods_id)
				this.body = ResponObj("json", TagCon.get(tag_id).getDetail());
			} else {
				throwCusError("参数错误")
			}
		}
	}
}